<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 15:10
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\AssignmentLabelModel;
use App\Http\Model\AssignmentModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AssignmentController extends Controller
{
    /**
     * 施工项列表
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'professionId' => 'required|integer',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:1',
        ];
        $message = [
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('professionId', 'length', 'page', 'search');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $assignmentModel = new AssignmentModel();
            $lists = $assignmentModel->lists($param);
            $countLists = $assignmentModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 250101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 施工项详情
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取施工项参数失败',
            'id.integer' => '施工项参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $assignmentModel = new AssignmentModel();
            $this->data = $assignmentModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 250201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 添加施工项
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'professionId' => 'required|integer',
            'name' => 'required',
            'unitId' => 'required|integer',
            'order' => 'integer',
        ];
        $message = [
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'name.required' => '请输入施工项名称',
            'unitId.required' => '获取工种参数失败',
            'unitId.integer' => '工种参数类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['professionId', 'name', 'unitId', 'remark', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $assignmentModel = new AssignmentModel();
            $info = $assignmentModel->checkRepeat(['professionId' => $param['professionId'], 'name' => $param['name']]);
            if (!empty($info)) {
                throw new ApiException('当前工种下，请勿重复建立相同名称的施工项', 250302);
            }
            $result = $assignmentModel->insert($param);
            if (!$result) {
                throw new ApiException('保存失败，请稍后重试', 250303);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 250301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 编辑施工项信息
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'unitId' => 'required|integer',
            'order' => 'integer',
        ];
        $message = [
            'id.required' => '获取施工项参数失败',
            'id.integer' => '施工项参数类型错误',
            'name.required' => '请输入施工项名称',
            'unitId.required' => '获取工种参数失败',
            'unitId.integer' => '工种参数类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['id', 'name', 'unitId', 'remark', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $assignmentModel = new AssignmentModel();
            $info = $assignmentModel->info(['id' => $param['id']]);
            $info = $assignmentModel->checkRepeat(['professionId' => $info['professionId'], 'name' => $param['name']], $param['id']);
            if (!empty($info)) {
                throw new ApiException('当前工种下，请勿重复建立相同名称的施工项', 250402);
            }
            if (!isset($param['order']) || empty($param['order'])) {
                $order = 1;
            } else {
                $order = $param['order'];
            }
            $updateData = [
                'name' => $param['name'],
                'unitId' => $param['unitId'],
                'remark' => $param['remark'],
                'order' => $order,
            ];
            $assignmentModel->update(['id' => $param['id']], $updateData);
        } else {
            throw new ApiException($validator->errors()->first(), 250401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 更新施工项状态
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'status' => 'required|integer|in:0,1',
        ];
        $message = [
            'id.required' => '获取施工项参数失败',
            'id.integer' => '施工项参数类型错误',
            'status.required' => '未获取到状态',
            'status.integer' => '施工项状态参数类型错误',
            'status.in' => '施工项状态参数值不正确',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $assignmentModel = new AssignmentModel();
            $assignmentModel->update(['id' => $param['id']], ['status' => $param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(), 250501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function label(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'labelId' => 'nullable|integer',
        ];
        $message = [
            'id.required' => '获取工种参数失败',
            'id.integer' => '工种参数类型错误',
            'labelId.integer' => '标签参数类型错误',
        ];
        $param = $request->only('id', 'labelId', 'name');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $assignmentLabelModel = new AssignmentLabelModel();
            if (!empty($param['name'])) {
                $info = $assignmentLabelModel->checkRepeat(['id' => $param['labelId']]);
                if (!empty($info) && $info['id'] != 1) {
                    $labelId = $info['id'];
                } else {
                    $labelId = $assignmentLabelModel->insert(['name' => $param['name']]);
                }
            }else{
                if (!empty($param['labelId'])){
                    $labelId = $param['labelId'];
                }else{
                    throw new ApiException($validator->errors()->first(), 250602);
                }
            }
            $assignmentModel = new AssignmentModel();
            $assignmentModel->update(['id'=>$param['id']],['labelId'=>$labelId]);
        } else {
            throw new ApiException($validator->errors()->first(), 250601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    public function labelLists(Request $request){
        $assignmentLabelModel = new AssignmentLabelModel();
        $this->data = $assignmentLabelModel->lists();
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}